import React from 'react';
import { inject, observer} from 'mobx-react';
import * as mobx from 'mobx';
import { Button ,message} from 'antd';
import * as _ from '../../util/public/common';
import cloneDeep from 'lodash/cloneDeep';
import { getAntd, getEcCom, getComsMobx } from '../../util/convert';

const { toJS } = mobx;
const {WeaLocaleProvider} = getEcCom();
const getLabel = WeaLocaleProvider.getLabel;

@inject('globalStore', 'freeWorkflowNewStore')
@observer
export default class ImportModelDialog extends React.Component {
  constructor(props) {
    super(props);
  }
  doInit(props){}
  componentDidMount() {
    this.doInit(this.props);
  }
  componentWillReceiveProps(nextProps) {}
  render() {
    const { freeWorkflowNewStore} = this.props;
    const { showImportModalDialog,setProperty,importModalStore,showImportModalAdvance,importModelForm,searchExistedModelList,onOperatesClick} = freeWorkflowNewStore;
    const {WeaDialog,WeaTab} = getEcCom();

    if(!WeaDialog){
      return <div></div>
    }
    let AdFormParams = {name:""};
    if(importModelForm){
      AdFormParams = importModelForm.getFormParams();
    }
    const {WeaTableNew} = getComsMobx();
    const WeaTable = WeaTableNew.WeaTable;
    return (
        <WeaDialog
            visible={showImportModalDialog}
            title={getLabel(23039,'选择模板')}
            icon="icon-coms-workflow"
            iconBgcolor='#0079DE'
            maskClosable={false}
            buttons={this.getModalBtns()}
            onCancel={() => setProperty({showImportModalDialog:false})}
            style={{width: "800px",height:'400px'}}
            hasScroll
        >
          <div className="wf-free-flowset-importModal">
            <WeaTab
                buttonsAd={this.getListAdButtons()}
                searchType={['base','advanced']}
                advanceHeight = {75}
                searchsBaseValue={AdFormParams.name||""}
                setShowSearchAd={bool=>{setProperty({showImportModalAdvance:bool})}}
                hideSearchAd={()=> setProperty({showImportModalAdvance:false})}
                searchsAd={this.getCusAdvancedSearch()}
                showSearchAd={showImportModalAdvance}
                onSearch={v=>{searchExistedModelList()}}
                onSearchChange={v=>{importModelForm.updateFields({name:{value:v}})}}
            />
            <WeaTable
                comsWeaTableStore={importModalStore}
                hasOrder={true}
                needRowClick={true}
                scroll={{y: 265}}
                onOperatesClick={onOperatesClick}
            />
          </div>
        </WeaDialog>
    )
  }
  getAdvancedSearch = () => {
    const { freeWorkflowNewStore } = this.props;
    const { importModelForm } = freeWorkflowNewStore;
    const coms = importModelForm.render({
      needTigger: false,
      showGroup:true,
      showTitle:false
    }, {
    });
    return coms;
  }
  getCusAdvancedSearch() {
    const {WeaFormItem,WeaSearchGroup,WeaSwitch} = getEcCom();
    const { freeWorkflowNewStore } = this.props;
    const { importModelForm, modelAdCondition } = freeWorkflowNewStore;
    if(!importModelForm){
      return <div></div>
    }
    const {isFormInit} = importModelForm;
    let group = [];
    isFormInit && toJS(modelAdCondition).map(c =>{
      let items = [];
      c.items.map(field => {
        let col = 2;
        if (field.conditionType.toUpperCase() == "CASCADER") col = 1;
        items.push({
          com:(<WeaFormItem
              label={`${field.label}`}
              labelCol={{span: `${field.labelcol}`}}
              ratio1to2
              error={importModelForm.getError(field)}
              tipPosition="bottom"
              wrapperCol={{span: `${field.fieldcol}`}}>
            <WeaSwitch fieldConfig={field} form={importModelForm}/>
          </WeaFormItem>),
          col:col,
          hide: importModelForm.isHide(field, (keys, allParams)=> {
            return false;
          })
        })
      });
      group.push(<WeaSearchGroup needTigger={false} showTitle = {false} showGroup={true} items={items}/>)
    });
    return group;
  }
  confirmSelection = () => {
    const {Modal,message} = getAntd();
    const { freeWorkflowNewStore} = this.props;
    const { importModalStore, postImportModelDatas,currentPostParams = {},freeNodeDatas={},isFree=""} = freeWorkflowNewStore;
    const selectedRowKeys = importModalStore.selectedRowKeys;
    if(_.isEmpty(toJS(selectedRowKeys))){
      message.warning(getLabel(506881,"请先选择模板"));
      return;
    }

    //未设置过自由节点数据则直接导入
    if(_.isEmpty(toJS(freeNodeDatas))){
      Modal.confirm({
        title: getLabel('131329', '信息确认'),
        content: getLabel(535049, `是否继续加签？`),
        onOk: () => {
          postImportModelDatas({targetTemplateid:selectedRowKeys[0],isFree});
        },
        okText: getLabel(83446,'确定'),
        okCancel : () => {},
      });
    }else{
      Modal.confirm({
        title: getLabel('131329', '信息确认'),
        content: `${isFree=='1' ? getLabel(506882,'导入模板将替换已设置的自由节点，是否继续？') : getLabel(506883,'导入模板将替换加签流转，是否继续？')}`,
        onOk: () => {
          postImportModelDatas({targetTemplateid:selectedRowKeys[0],isFree});
        },
        okText: getLabel(83446,'确定'),
        okCancel : () => {},
      });
    }
  }
  getListAdButtons(){
    const {Button} = getAntd();
    const { freeWorkflowNewStore } = this.props;
    const {searchExistedModelList,setProperty,importModelForm} = freeWorkflowNewStore;
    return [
      (<Button type="primary" onClick={()=>{searchExistedModelList();setProperty({showImportModalAdvance:false});}}>{getLabel(197,'搜索')}</Button>),
      (<Button type="ghost" onClick={()=>{importModelForm.resetConditionValue();}}>{getLabel(2022,'重置')}</Button>),
      (<Button type="ghost" onClick={()=>{setProperty({showImportModalAdvance:false})}}>{getLabel(31129,'取消')}</Button>)
    ];
  }
  getModalBtns(){
    const {Button} = getAntd();
    const { freeWorkflowNewStore } = this.props;
    const { setModelImportDialogShow ,loading = false,setProperty} = freeWorkflowNewStore;
    let btns = [];
    btns.push(<Button type="primary" loading={loading} onClick={()=>{
      this.confirmSelection();
    }}>{getLabel(83446,'确定')}</Button>);
    btns.push(<Button type="primary" loading={loading} onClick={()=>{setProperty({showImportModalDialog:false})}}>{getLabel(201,'取消')}</Button>);
    return btns;
  }
}
